package com.fart.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fart.common.R;
import com.fart.domain.Employee;
import com.fart.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        //1.接收请求参数
        String password = employee.getPassword();
        //1.1对代码进行md5加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2.根据用户查询信息 -> 条件查询 ->构建条件构造器
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        //等值查询lqw.eq
        lqw.eq(Employee::getUsername,employee.getUsername());
        //调用service方法
        Employee emp = employeeService.getOne(lqw);
        //2.1没有查询用户，则登陆失败
        if(emp == null){
            return R.error("登录失败，无法查询到该用户");
        }
        //2.2查询到用户，则进行密码对比
        if(!emp.getPassword().equals(password)){
            return R.error("登录失败，密码不正确");
        }
        //查询员工状态
        if(emp.getStatus() == 0){
            return R.error("账号被禁用");
        }
        //将员工信息存入session中
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //1.清除session中的用户信息
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        log.info("新增员工:{}",employee.toString());
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        Long empId = (Long)request.getSession().getAttribute("employee");
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);

        employeeService.save(employee);
        return R.success("成功新增用户");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        //构造分页条件
        Page pageInfo = new Page<>(page,pageSize);
        //构建搜索条件
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null,Employee::getName,name);
        lqw.orderByDesc(Employee::getUpdateTime);
        //执行查询
        employeeService.page(pageInfo,lqw);
        //封装结果并返回
        return R.success(pageInfo);
    }
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        //log.info(employee.toString());
        //id: 1582267649660403700 != 1582194509492043778  status:0
        long id = Thread.currentThread().getId();
        log.info("线程id:{}",id);
        //1.从session取出用户信息
        //Long empId = (Long)request.getSession().getAttribute("employee");
        //2.修改时间,修改人
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(empId);

        //3.调用service中的更新方法
        employeeService.updateById(employee);

        return R.success("员工信息修改成功");
    }

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        //测试

        //1.调用service进行查询用户
        Employee emp = employeeService.getById(id);
        return R.success(emp);
    }
}
